package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;

import com.xuexiang.xui_lib.component.dialog.building.DialogBuilder;
import com.xuexiang.xui_lib.component.dialog.config.ChooseBean;
import com.xuexiang.xui_lib.component.dialog.config.ConfigBean;
import com.xuexiang.xui_lib.component.dialog.interfaces.ItemDialogListener;
import com.xuexiang.xui_lib.component.dialog.view.StyledDialog;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;

import java.util.ArrayList;

/**
 * BottomSheetDialog底部弹出窗
 *
 * @since 2021-04-08
 */
public class BottomSheetAbilitySlice extends AbilitySlice implements Component.ClickedListener{
    private Image image_back, image_save;
    private DirectionalLayout dl_one, dl_two;
    private Text tv_chat, tv_friend, tv_moment, tv_weibo, tv_close;
    ConfigBean configBean = null;
    DirectionalLayout dl_bottom_sheet;
    private AnimatorProperty animOutBOTTOM;
    private CommonDialog dlg;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bottom_sheet);
        initView();
        initClickedListener();
    }

    public void initView() {
        image_back = (Image) findComponentById(ResourceTable.Id_image_back);
        dl_one = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_one);
        dl_two = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_two);
        dl_bottom_sheet = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_bottom_sheet);
    }

    private void initClickedListener() {
        image_back.setClickedListener(this);
        dl_one.setClickedListener(this);
        dl_two.setClickedListener(this);
        dl_bottom_sheet.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_dl_one:
                CharSequence[] words = {"Item 1", "Item 2", "Item 3"};
                boolean[] selectItems = {true, false, false};

                ArrayList<ChooseBean> list = new ArrayList<>();
                for (int i = 0; i < words.length; i++) {
                    ChooseBean chooseBean = new ChooseBean();
                    chooseBean.setTxt(words[i]);
                    chooseBean.setChoosen(selectItems[i]);
                    list.add(chooseBean);
                }
                configBean = StyledDialog.buildMdIOSList(getContext(), list, new ItemDialogListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        if (position == 0) {
                            ToastUtil.showToast(getContext(), "Item 1");
                        } else if (position == 1) {
                            ToastUtil.showToast(getContext(), "Item 2");
                        } else {
                            ToastUtil.showToast(getContext(), "Item 3");
                        }
                        configBean.commonDialog.hide();
                    }

                    @Override
                    public void positiveButton() {
                        if (configBean.commonDialog != null) {
                            configBean.commonDialog.hide();
                            configBean.commonDialog = null;
                        }

                    }

                    @Override
                    public void negativeButton() {
                        if (configBean.commonDialog != null) {
                            configBean.commonDialog.hide();
                            configBean.commonDialog = null;
                        }
                    }
                });
                configBean.commonDialog.show();
                configBean.property.start();
                break;
            case ResourceTable.Id_dl_two:
                dlg = new CommonDialog(this);
                Component layout = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ios_list_single_share_layout, null, true);
                AnimatorProperty property = createAnimBottom();
                animOutBOTTOM = createAnimOutBottom();
                dlg.setTransparent(true);
                dlg.setContentCustomComponent(layout);
                dlg.setAutoClosable(true);
                property.setTarget(layout);
                animOutBOTTOM.setTarget(layout);
                Component.ClickedListener listener = new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ToastUtil.showToast(getContext(), ((Text) component).getText());
                        dismissDialog();
                    }
                };
                tv_chat = (Text) layout.findComponentById(ResourceTable.Id_tv_chat);
                tv_chat.setClickedListener(listener);
                tv_friend = (Text) layout.findComponentById(ResourceTable.Id_tv_friend);
                tv_friend.setClickedListener(listener);
                tv_moment = (Text) layout.findComponentById(ResourceTable.Id_tv_moment);
                tv_moment.setClickedListener(listener);
                tv_weibo = (Text) layout.findComponentById(ResourceTable.Id_tv_weibo);
                tv_weibo.setClickedListener(listener);
                tv_close = (Text) layout.findComponentById(ResourceTable.Id_ios_single_share_close_tv);
                tv_close.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        dismissDialog();
                    }
                });
                image_save = (Image) layout.findComponentById(ResourceTable.Id_image_save);
                image_save.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ToastUtil.showToast(getContext(), "保存到本地");
                        dismissDialog();
                    }
                });
                DependentLayout dp_chat = (DependentLayout) layout.findComponentById(ResourceTable.Id_dp_chat);
                dp_chat.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        dismissDialog();
                    }
                });
                dlg.show();
                property.start();
                break;
        }
    }

    private AnimatorProperty createAnimBottom() {
        AnimatorProperty animator = new AnimatorProperty();
        animator.moveFromY(200).moveToY(0).alphaFrom(0).alpha(1).setDuration(300);
        return animator;
    }

    private AnimatorProperty createAnimOutBottom() {
        AnimatorProperty animator = new AnimatorProperty();
        animator.moveFromY(0).moveToY(200).alphaFrom(1).alpha(0).setDuration(300);
        return animator;
    }

    private void dismissDialog() {
        animOutBOTTOM.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                if (dlg != null) {
                    dlg.destroy();
                }
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        }).start();
    }
}

